clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"

/* share immigrants in CZE */
use "ImmPanelRevis19\Donnees\pit\instru2017R21.dta", clear
keep an ze zeimmig mlsnd zepop
rename ze aze
rename mlsnd amlsnd 
rename zepop azepop
save tempAZE.dta, replace

use "ImmPanelRevis19\donnees\indivm.dta", clear
/***************************************************/
/* Construct the table final.dta for the estimates */
/***************************************************/
/**************************************/
/* Définition des zones géographiques */
/**************************************/
/**************************************/
/* 1) => Merge avec les ZE 			  */
/**************************************/
/* elimine la corse */
drop if dep=="2A" | dep=="2B"

gen cgeo=dep+comt
sort cgeo
/* Paris */
replace cgeo="75056" if dep=="75"
/* Lyon */
replace cgeo="69123" if cgeo=="69381" | cgeo=="69382" | cgeo=="69383" | cgeo=="69384" | cgeo=="69385" | cgeo=="69386" | cgeo=="69387" | cgeo=="69388" | cgeo=="69389" 
/* Marseille */
replace cgeo="13055" if cgeo=="13201" | cgeo=="13202" | cgeo=="13203" | cgeo=="13204" | cgeo=="13205" | cgeo=="13206" | cgeo=="13207" | cgeo=="13208" | cgeo=="13209" | cgeo=="13210" | cgeo=="13211" | cgeo=="13212" | cgeo=="13213" | cgeo=="13214" | cgeo=="13215" | cgeo=="13216"

/* définit les ZE */
joinby cgeo using "ImmPanelRevis19\donnees\cgeo_ze.dta", unm(m)
tab _merge
drop if _merge==1
drop _merge
drop zen

/* foreign born workers */
gen fborn=(depnai=="99")
gen ifborn="N" if fborn==0
replace ifborn="FB" if fborn==1
drop if fborn

/*********************/
/* add task contents */
/*********************/
joinby cs2h using "ImmPanelRevis19\Donnees\Onet\CS2_OnetNorm.dta", unm(m)
tab _merge
drop _merge

save temp.dta, replace

/***************************/
/* Add contemporary groups */
/***************************/
use "ImmPanelRevis19\donnees\ident_groupmw.dta", clear
rename gref an
save gref.dta, replace

use temp.dta, clear
joinby nninouv an using gref.dta, unm(m)
tab _merge
drop _merge

egen time=group(an)
xtset indiv time
compress

save temp.dta, replace

/******************************************/
/* Merge with immigration shocks location */
/******************************************/
joinby ze an using "ImmPanelRevis19\Donnees\pit\instru2017R21.dta", unm(m)
tab _merge
drop _merge


/*********************/
/* change in outcome */
/*********************/
egen indiv2=group(nninouv)
xtset indiv2 time
sort indiv2 time

/**************/
/* occupation */
/**************/
/* Changes in task intensity */
gen dzbasman=D.zbasman 
gen dzbascom=D.zbascom 

/* fixed effects */
xi, pre(Y_) i.an*lsconst i.an*lstrad i.an*lsntrad i.an*lzepop

/* location shifter */
egen ize=group(ze)
gen lshift=(ize!=L.ize)
/* eliminate small CZ */
drop if ze=="9306" | ze=="9109" | ze=="9110" | ze=="9116" | ze=="8307" | ze=="7206" | ze=="5214" 
drop if ze=="4101" | ze=="4111" | ze=="8306"

keep if age>=25 & age<=59
/* fixed effects age x an */
xi i.age*i.an , pre(F_)

/* first period of obs treated */
gen ptreat=(age>=25 & age<=50)
/* second period of obs treated */
gen dtreat=(age>=31 & age<=56) if an==1982
replace dtreat=(age>=34 & age<=59) if an==1991
replace dtreat=(age>=33 & age<=58) if an==1999
replace dtreat=(age>=33 & age<=58) if an==2007

gen tecemp2=(tec==1 | emp2==1)

save "ImmPanelRevis19\Donnees\select\unbalanced.dta", replace
